/*
 * Created on 2005/1/18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.erp.app.common.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.http.HttpSession;

import com.erp.app.admin.projParam.vo.ProjectParamsList;
import com.erp.app.admin.sysParam.beans.SysParamList;
import com.erp.app.admin.sysParam.beans.SysParamVO;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.user.User;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class CompanyUtil implements Serializable {
    public CompanyUtil() {
    }

    public static String getUserName(HttpSession session) {

        User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        /*
         * ArrayList userDataAccessInfo =
         * (ArrayList)session.getAttribute(Constants.SESSION_USER_DATA_ACCESS_SECURITY);
         * 
         * String userName = ""; if(userDataAccessInfo != null) { userName =
         * (String)((Hashtable)userDataAccessInfo.get(0)).get("USER_NAME"); }
         * else { userName = userid; }
         */
        String userName = user.getUserName();
        return userName;
    }

    public static String getUserDept(HttpSession session) {

        User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        String dept = user.getDeptDesc();

        return dept;
    }

    public static String getUserGroupCode(HttpSession session) {

        User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        String userGroupCode = user.getUserGroupCode();

        return userGroupCode;
    }

    public static String getAppropriateCompanyCode(SimpleBaseSrchCriteria criteria, String paramId) {
        SysParamList sysParam = criteria.getSysParamList();
        String newCode = criteria.getLoginCompanyCode();

        if (paramId == null || sysParam == null)
            return newCode;

        ArrayList paramList = sysParam.getSysParamList();

        if (paramList == null)
            return newCode;

        for (int i = 0; i < paramList.size(); i++) {
            SysParamVO vo = (SysParamVO) paramList.get(i);
            if (paramId.equals(vo.getParamId())) {
                if (ProjectConstants.GENERIC_YES.equals(vo.getParamVal()))
                    newCode = sysParam.getParamValById(ProjectConstants.SP_GENERAL_COMPANY_CODE);
                break;
            }
        }

        if (newCode == null)
            newCode = "";

        return newCode;
    }

    public static String getSysParamVal(HttpSession session, String paramId) {
        String paramVal = "";

        if (session == null)
            return paramVal;
        if (paramId == null)
            return paramVal;

        SysParamList sysParam = (SysParamList) session.getAttribute(ProjectConstants.SESSION_SYS_PARAM);

        if (sysParam == null)
            return paramVal;

        ArrayList paramList = sysParam.getSysParamList();
        if (paramList == null)
            return paramVal;

        paramVal = sysParam.getParamValById(paramId);
        paramVal = (paramVal == null) ? "" : paramVal;

        return paramVal;
    }

    public static String getPrjParamVal(HttpSession session, String paramId) {
        String paramVal = "";

        if (session == null)
            return paramVal;
        if (paramId == null)
            return paramVal;

        ProjectParamsList projectParam = (ProjectParamsList) session.getAttribute(ProjectConstants.SESSION_PROJECT_PARAM);

        if (projectParam == null)
            return paramVal;

        ArrayList paramList = projectParam.getProjectParamsList();
        if (paramList == null)
            return paramVal;

        paramVal = projectParam.getPrjParamValById(paramId);
        paramVal = (paramVal == null) ? "" : paramVal;

        return paramVal;
    }

    public static String getCodeSysParamId(String masterRefCode) {
        String paramId = "";
        paramId = "SP_SHARE_" + CommonUtil.nullToString(masterRefCode).toUpperCase();

        return paramId;
    }

    public static String getDataAccessSecurityWhereClause(HttpSession session, String deptFieldName, String userFieldName) {
        ArrayList userDataAccessInfo = (ArrayList) session.getAttribute(ProjectConstants.SESSION_USER_DATA_ACCESS_SECURITY);
        // String orderDept = "";
        // ArrayList resultList = new ArrayList();

        String allWhere = "";
        String ownerWhere = "";
        String otherWhere = "";

        if ((userDataAccessInfo != null) && (!userDataAccessInfo.isEmpty())) {
            for (int i = 0; i < userDataAccessInfo.size(); i++) {
                Hashtable hash = (Hashtable) userDataAccessInfo.get(i);
                String accessStatus = (String) hash.get("ACCESS_STATUS");
                String accessUserCode = (String) hash.get("ACCESS_USER_CODE");
                String accessDept = (String) hash.get("ACCESS_DEPT");
                String userCode = (String) hash.get("USER_CODE");
                if ((ProjectConstants.GENERIC_YES.equals(accessStatus)) && (ProjectConstants.ACCESS_ALL.equals(accessUserCode))) {
                    if ("".equals(allWhere)) {
                        allWhere = "(" + deptFieldName + "='" + accessDept + "')";
                    } else {
                        allWhere += " OR (" + deptFieldName + "='" + accessDept + "')";
                    }
                } else if ((ProjectConstants.GENERIC_YES.equals(accessStatus)) && (ProjectConstants.ACCESS_OWNER.equals(accessUserCode))) {
                    if ("".equals(ownerWhere)) {
                        ownerWhere = userFieldName + "='" + userCode + "' and " + deptFieldName + "='" + accessDept + "'";
                    }
                } else if (ProjectConstants.GENERIC_YES.equals(accessStatus)) {
                    if ("".equals(otherWhere)) {
                        otherWhere = "(" + userFieldName + "='" + accessUserCode + "' AND " + deptFieldName + "='" + accessDept
                                + "')";
                    } else {
                        otherWhere += " OR (" + userFieldName + "='" + accessUserCode + "' AND " + deptFieldName + "='"
                                + accessDept + "')";
                    }

                }
            }
        } else {
            System.out.println("WebUtil.getDataAccessSecurityWhereClause(): " + "the User Data Access Info ArrayList is empty.");
        }

        String tmpWhere = "";
        if (!"".equals(allWhere)) {
            tmpWhere = allWhere;
        }

        if (!"".equals(ownerWhere)) {
            if ("".equals(tmpWhere)) {
                tmpWhere = ownerWhere;
            } else {
                tmpWhere = tmpWhere + " OR (" + ownerWhere + ")";
            }
        }

        if (!"".equals(otherWhere)) {
            if ("".equals(tmpWhere)) {
                tmpWhere = otherWhere;
            } else {
                tmpWhere = tmpWhere + " OR (" + otherWhere + ")";
            }
        }

        return tmpWhere;
    }

    public static String getDataAccessSecurityWhereClause(HttpSession session, String deptFieldName) {
        ArrayList userDataAccessInfo = (ArrayList) session.getAttribute(ProjectConstants.SESSION_USER_DATA_ACCESS_SECURITY);
        // String orderDept = "";
        // ArrayList resultList = new ArrayList();

        // where = "ORDER_DEPT in ('112233', '333', '1122')
        String allWhere = "";
        String ownerWhere = "";
        String otherWhere = "";

        if ((userDataAccessInfo != null) && (!userDataAccessInfo.isEmpty())) {
            for (int i = 0; i < userDataAccessInfo.size(); i++) {
                java.util.Hashtable hash = (java.util.Hashtable) userDataAccessInfo.get(i);
                String accessStatus = (String) hash.get("ACCESS_STATUS");
                String accessUserCode = (String) hash.get("ACCESS_USER_CODE");
                String accessDept = (String) hash.get("ACCESS_DEPT");
                String userCode = (String) hash.get("USER_CODE");
                if ((ProjectConstants.GENERIC_YES.equals(accessStatus)) && (ProjectConstants.ACCESS_ALL.equals(accessUserCode))) {
                    if ("".equals(allWhere)) {
                        allWhere = "(" + deptFieldName + "='" + accessDept + "')";
                    } else {
                        allWhere += " OR (" + deptFieldName + "='" + accessDept + "')";
                    }
                } else if ((ProjectConstants.GENERIC_YES.equals(accessStatus)) && (ProjectConstants.ACCESS_OWNER.equals(accessUserCode))) {
                    if ("".equals(ownerWhere)) {
                        ownerWhere = deptFieldName + "='" + accessDept + "'";
                    }
                } else if (ProjectConstants.GENERIC_YES.equals(accessStatus)) {
                    if ("".equals(otherWhere)) {
                        otherWhere = "(" + deptFieldName + "='" + accessDept + "')";
                    } else {
                        otherWhere += " OR (" + deptFieldName + "='" + accessDept + "')";
                    }

                }
            }
        } else {
            System.out.println("WebUtil.getDataAccessSecurityWhereClause(): " + "the User Data Access Info ArrayList is empty.");
        }

        String tmpWhere = "";
        if (!"".equals(allWhere)) {
            tmpWhere = allWhere;
        }

        if (!"".equals(ownerWhere)) {
            if ("".equals(tmpWhere)) {
                tmpWhere = ownerWhere;
            } else {
                tmpWhere = tmpWhere + " OR (" + ownerWhere + ")";
            }
        }

        if (!"".equals(otherWhere)) {
            if ("".equals(tmpWhere)) {
                tmpWhere = otherWhere;
            } else {
                tmpWhere = tmpWhere + " OR (" + otherWhere + ")";
            }
        }

        return tmpWhere;
    }
}
